package com.dy.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.security.entity.UserEntity;
import com.dy.security.mapper.UserMapper;
import com.dy.security.service.UserService;
import com.dy.sucetity.exception.BadRequestException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @Author: ding-yu
 * @Date: 2022/5/24 10:54
 * @Desctiption: (描述)
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService, UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查看当前用户是否存在
        UserEntity one = this.getOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUsername, username));
        if (ObjectUtils.isEmpty(one)) {
            throw new BadRequestException("用户不存在");
        }
        return one;
    }
}
